'''功能层'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import sys

sys.path.append(r'..')

from web_base_discuz.base_page import BasePage
class DiscuzPage(BasePage):
    url = r'http://192.168.190.130/upload/'
    #登录定位
    username_loc = (By.ID, 'ls_username')
    password_loc = (By.ID, 'ls_password')
    login_btn_loc = (By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button')

    #退出定位
    logout_btn_loc = (By.XPATH, '//*[@id="um"]/p[1]/a[5]')
    #注册
    register_btn_loc = (By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[4]/a')
    newname_loc = (By.ID, 'wIDZp8')
    pwd_loc = (By.ID, 'vqL88k')
    pwd2_loc = (By.ID, 'Iyz22Y')
    email_loc = (By.ID, 'xOmGZG')
    register_submit_btn_loc = (By.ID, 'registerformsubmit')

    #看帖
    fid_btn_loc = (By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a')  #点击默认版块
    tid_btn_loc = (By.XPATH, '//*[@id="normalthread_59"]/tr/th/a[2]')
    #发帖
    newtid_btn_loc = (By.XPATH, '//*[@id="newspecial"]/img')   #点击发帖
    title_loc = (By.ID, 'subject')
    iframe_loc = (By.ID, 'ajaxpostframe')
    kw_loc = (By.XPATH, '/html/body')
    submit_btn_loc = (By.ID, 'postsubmit')

    #回帖
    reply_btn_loc = (By.XPATH, '//*[@id="post_reply"]/img')
    reply_kw_loc = (By.ID, 'postmessage')
    reply_submit_btn_loc = (By.ID, 'postsubmit')

    def __init__(self,dr):
        super().__init__(dr)
        self.get_url(self.url)
    def login(self,username,password):
        self.clear_ele(self.username_loc)
        self.input_ele(self.username_loc, username)
        self.clear_ele(self.password_loc)
        self.input_ele(self.password_loc, password)
        self.click_ele(self.login_btn_loc)
        sleep(3)
    def logout(self):
        self.click_ele(self.logout_btn_loc)
        sleep(1)
    def register(self,newname,pwd,pwd2,email):
        self.click_ele(self.register_btn_loc)
        sleep(2)
        self.input_ele(self.newname_loc, newname)
        sleep(2)
        self.input_ele(self.pwd_loc, pwd)
        sleep(1)
        self.input_ele(self.pwd2_loc, pwd2)
        sleep(1)
        self.input_ele(self.email_loc, email)
        sleep(2)
        self.click_ele(self.register_submit_btn_loc)
        sleep(5)
    def read(self):
        self.click_ele(self.fid_btn_loc)
        sleep(1)
        self.click_ele(self.tid_btn_loc)
        sleep(1)
    def send(self,username,password,title,kw):
        self.login(username, password)    #登录
        self.click_ele(self.fid_btn_loc)   #点击默认版块
        sleep(1)
        self.click_ele(self.newtid_btn_loc)  #点击发新帖
        self.input_ele(self.title_loc, title)    #输入标题
        sleep(2)
        self.switchto_ele(self.iframe_loc)      #跳转到iframe框架
        self.input_ele(self.kw_loc, kw)    #输入帖子内容
        sleep(2)
        self.switchout_ele()  #跳出iframe框架
        self.click_ele(self.submit_btn_loc)   #点击发布新帖
        sleep(5)
    def reply(self,username,password,reply_kw):
        self.login(username, password)  # 登录
        self.read()
        sleep(1)
        self.click_ele(self.reply_btn_loc)
        sleep(1)
        self.input_ele(self.reply_kw_loc, reply_kw)
        sleep(1)
        self.click_ele(self.reply_submit_btn_loc)  #提交回帖内容
        sleep(2)

if __name__ == '__main__':
    username = 'test01'
    password = '123456'
    newname = 'start01'
    pwd = '123456'
    pwd2 = '123456'
    email = 'star01@qq.com'
    title = '我的第一条帖子哈'
    kw = '我的第一条帖子我的第一条帖子我的第一条帖子'
    reply_kw = '我的第一条回复我的第一条回复我的第一条回复'

    chrome_location = r'D:\chrom browser\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location
    dr = webdriver.Chrome(options=options)
    dr.maximize_window()
    discuz_page = DiscuzPage(dr)
    # discuz_page.assert(username, password)
    # discuz_page.register(newname,pwd,pwd2,email)
    # discuz_page.send(username,password,title,kw)
    # discuz_page.read()
    discuz_page.reply(username,password,reply_kw)
    dr.quit()












